﻿using System;
using System.IO;
using System.IO.Compression;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<string> files = new List<string>
        {
            "D:\\1.txt",
            "D:\\2.txt",
        };

        byte[] zipBytes = CreateZipArchive(files);
        string zipFilePath = @"D:\example.zip";
        File.WriteAllBytes(zipFilePath, zipBytes);

        Console.WriteLine("Zip file saved to: " + zipFilePath);
        
    }

    static byte[] CreateZipArchive(List<string> files)
    {
        using (MemoryStream memoryStream = new MemoryStream())
        {
            using (ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
            {
                foreach (string filePath in files)
                {
                    string fileName = Path.GetFileName(filePath);
                    if (fileName != null)
                    {
                        // 在压缩包中创建一个条目，并将文件写入该条目
                        ZipArchiveEntry entry = archive.CreateEntry(fileName);
                        using (Stream entryStream = entry.Open())
                        using (FileStream fileStream = File.OpenRead(filePath))
                        {
                            fileStream.CopyTo(entryStream);
                        }
                    }
                }
            }

            // 将内存流转换为字节数组并返回
            return memoryStream.ToArray();
        }
    }
}